package ;

import flash.display.Sprite;
import flash.geom.Point;

/**
 * ...
 * @author 
 */

class Movable extends Sprite
{
	var mLifeTime: Int;
	var mDeathCount: Int;
	
	var mDX: Float;
	var mDY: Float;
	var mMoveCount: Int;
	var mTargetPos: Point;

	var mDAlpha: Float;
	var mBlendCount: Int;

	public function new(pos: Point) 
	{
		super();
		
		x = pos.x;
		y = pos.y;
		
		mLifeTime = mDeathCount = -1;
		
		mDX = mDY = 0;
		mMoveCount = -1;
		
		mDAlpha = 0;
		mBlendCount = -1;
	}
	
	
	public function SetLifeTime(frames: Int)
	{
		mLifeTime = mDeathCount = frames;
	}
	
	
	inline public function GetLifeTime(): Int
	{
		return mLifeTime;
	}
	
	
	public function SetSpeed(dx: Float, dy: Float)
	{
		mDX = dx;
		mDY = dy;
	}
	
	
	public function SetBlending(start: Float, end: Float, time: Int)
	{
		alpha = start;
		mDAlpha = 0;
		
		mBlendCount = time;
		if (mBlendCount > 0) {
			mDAlpha = (end - start) / mBlendCount;
		}
	}
	
	
	public function MoveTo(target: Point, time: Int)
	{
		mMoveCount = time;
		mTargetPos = target;
		
		if (mMoveCount > 0) {
			mDX = (target.x - x) / mMoveCount;
			mDY = (target.y - y) / mMoveCount;
			
			//trace("movement: dx " + mDX);
			//trace("movement: dy " + mDY);
		}
	}
	
	
	public function Update(framesAfterUpdate: Int, framesBetweenUpdates: Int): Bool
	{				
		// life and death
		if (mDeathCount == 0)
			return false;
		
		if (mDeathCount > 0)
			mDeathCount--;
			

		// blending
		if (mBlendCount > 0)
			mBlendCount--;
		
		if (mDAlpha != 0) {
			alpha += mDAlpha;
			if (alpha > 1) alpha = 1;
			else if (alpha < 0) alpha = 0;
		}
		
		if (mBlendCount == 0) {
			mBlendCount = -1;
			mDAlpha = 0;
		}
			
			
		// movements
		if (mMoveCount > 0)
			mMoveCount--;
			
		x += mDX;
		y += mDY;

		if (mMoveCount == 0) {
			mMoveCount = -1;
			mDX = mDY = 0;
			x = mTargetPos.x;
			y = mTargetPos.y;
		}
		
		return true;
	}
	
	
	public function Draw()
	{
		
	}
	
}